<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class Reply extends Model
{

    use HasDateTimeFormatter;

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->select(['id', 'name', 'head_img', 'email']);
    }

    public function toUser()
    {
        return $this->hasOne(User::class, 'id', 'to_user')->select(['id', 'name', 'head_img', 'email']);
    }

    // 返回当前页数据列和总数据量
    public function getPaginate($where = [], $with = [], $page = 1, $perPage = 10)
    {

        $data = $this->newQuery()
            ->when($where, function ($query, $where) {
                return $query->where($where);
            })
            ->when($with, function ($query, $with) {
                return $query->with($with);
            })
            ->orderBy('is_adopt', 'desc')
            ->orderBy('id', 'desc')
            ->paginate($perPage, ['*'], 'page', $page);

        //return ['list' => $data->items(), 'total' => $data->total()];
        return ['list' => $data->toArray()['data'], 'total' => $data->total()];
    }


}
